package com.tdam2011.grupo05.ui.activities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tdam2011.grupo05.control.app.TdamApplication;
import com.tdam2011.grupo05.services.MessageService;
import com.tdam2011.grupo05.services.constant.ServiceActions;
import com.tdam2011.grupo05.ui.activities.message.MessageWebActivity;
import com.tdam2011.grupo05.R;
import com.tdam2011.grupo05.ui.activities.config.preference.ConfigActivity;
import com.tdam2011.grupo05.ui.activities.contactos.ContactoActivity;
import com.tdam2011.grupo05.utils.preference.PreferencesHelper;

public class DashboardActivity extends Activity {
	/** Called when the activity is first created. */

	public static final String TAG = "DashboardActivity";
	private static final int REQUEST_ID_CONFIG = 1;
	// TODO
	private TextView titulo;
	private Button email_btn;
	private Button llamar_btn;
	private Button mensaje_btn;
	private ImageView contact_btn;
	private Button mensaje_web;

	private PreferencesHelper mHelper;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		Log.d(TAG, "Main.onCreate");

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dash_board);
		Typeface tf = Typeface.createFromAsset(getAssets(),
				"fonts/BIMINR__.TTF");

		mHelper = ((TdamApplication) getApplication()).getPreference();

		titulo = (TextView) findViewById(R.id.texto_titulo);
		titulo.setTypeface(tf);
		titulo.setTextSize(22);

		email_btn = (Button) findViewById(R.id.opcion_btn_email);
		email_btn.setTypeface(tf);
		email_btn.setTextSize(20);

		llamar_btn = (Button) findViewById(R.id.opcion_btn_llamar);
		llamar_btn.setTypeface(tf);
		llamar_btn.setTextSize(20);

		mensaje_btn = (Button) findViewById(R.id.opcion_btn_mensaje);
		mensaje_btn.setTypeface(tf);
		mensaje_btn.setTextSize(20);

		contact_btn = (ImageView) findViewById(R.id.contact_titulo);

		mensaje_web = (Button) findViewById(R.id.opcion_btn_sms_web);
		mensaje_web.setTypeface(tf);
		mensaje_web.setTextSize(20);

		email_btn.setOnClickListener(emailAction);

		llamar_btn.setOnClickListener(callAction);

		mensaje_btn.setOnClickListener(messageAction);

		contact_btn.setOnClickListener(contactAction);

		mensaje_web.setOnClickListener(messageWebAction);

	}

	private View.OnClickListener emailAction = new OnClickListener() {

		@Override
		public void onClick(View v) {
			openEmailActivity();
		}
	};

	private View.OnClickListener callAction = new OnClickListener() {

		@Override
		public void onClick(View v) {
			openLlamadaActivity();
		}
	};

	private View.OnClickListener messageWebAction = new OnClickListener() {

		@Override
		public void onClick(View v) {
			openMensajeWebActivity();
		}
	};

	private View.OnClickListener contactAction = new OnClickListener() {

		@Override
		public void onClick(View v) {
			openContactosActivity();
		}
	};

	private View.OnClickListener messageAction = new OnClickListener() {

		@Override
		public void onClick(View v) {
			openMensajeActivity();
		}
	};

	@Override
	protected void onStart() {
		super.onStart();

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQUEST_ID_CONFIG) {
			if (resultCode == RESULT_OK) {
				Log.d(TAG, "preference result ok");
				Intent resetService = new Intent(this, MessageService.class);
				resetService
						.setAction(ServiceActions.RESET_SERVICE_MESSAGE_ACTION);
				startService(resetService);

			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.dashboard_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.menu_exit:

			logout();
			break;
		case R.id.menu_configurar:
			openConfig();
			break;
		case R.id.menu_nueva_cuenta:
			changeAccount();
			break;
		default:
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	private void changeAccount() {
		mHelper.setInitialConfigState(false);
		mHelper.setSyncState(false);
		mHelper.saveAccountConfigStatus(false);
		stopService(new Intent(this, MessageService.class));
		Intent intent = new Intent(this, InicioActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
		finish();
	}

	private void openConfig() {
		final Intent intent = new Intent(DashboardActivity.this,
				ConfigActivity.class);
		startActivityForResult(intent, REQUEST_ID_CONFIG);
	}

	public void openEmailActivity() {
		final Intent i = new Intent(android.content.Intent.ACTION_SEND);
		// i.setType("text/plain");
		i.setType("png/image");
		String subject = "Email Subject";

		String body = "Cuerpo del Mensaje";

		i.putExtra(Intent.EXTRA_SUBJECT, subject);
		i.putExtra(Intent.EXTRA_TEXT, body);
		i.putExtra(Intent.EXTRA_EMAIL, new String[] { "droide" });

		try {
			startActivity(Intent.createChooser(i, "Enviar Mail..."));
		} catch (android.content.ActivityNotFoundException ex) {
			Toast.makeText(this, "No hay clientes de mail instalados.",
					Toast.LENGTH_SHORT).show();
		}

	}

	public void openLlamadaActivity() {
		Intent intent = new Intent(Intent.ACTION_DIAL);
		startActivity(intent);
	}

	public void openMensajeActivity() {
		Intent smsIntent = new Intent(Intent.ACTION_VIEW);
		smsIntent.setType("vnd.android-dir/mms-sms");
		startActivity(smsIntent);
	}

	public void openContactosActivity() {
		Intent intent = new Intent(this, ContactoActivity.class);
		startActivity(intent);
	}

	public void openMensajeWebActivity() {
		Intent intent = new Intent(this, MessageWebActivity.class);
		startActivity(intent);
	}

	public void logout() {
		mHelper.saveAccountConfigStatus(false);
		stopService(new Intent(this, MessageService.class));
		// Intent intent = new Intent(this, InicioActivity.class);
		// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		// startActivity(intent);
		finish();
	}
}